﻿using System;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Data;
using PlanningGIS.Framework.Config;
using PlanningGIS.Framework;
using PlanningGIS.ManageUtility.DataAccess;
using PlanningGIS.Util.Security;
using PlanningGIS.ManageUtility;
using PlanningGIS.Util.Regular;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>
    /// 修改密码
    /// </summary>
    public partial class FrmUpdatePSD : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmUpdatePSD()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmUpdatePSD_Load(object sender, EventArgs e)
        {
            customValidator1.ValidateValue += customValidator1_ValidateValue;
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void customValidator1_ValidateValue(object sender, DevComponents.DotNetBar.Validator.ValidateValueEventArgs e)
        {
            if (ManageHelper.SecuritySet.AllowFZD)
            {
                e.IsValid = VerifyHelper.ValidPassword(txtpsd.Text, 10, 3, 30);
            }
            else
            {
                e.IsValid = VerifyHelper.ValidPassword(txtpsd.Text);
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtOldPwd.Text))
            {
                MsgHelper.ShowWarningMsgbox("输入不能为空！");
                return;
            }
            if (string.IsNullOrWhiteSpace(txtpsd.Text))
            {
                MsgHelper.ShowWarningMsgbox("新密码不正确，请重新输入！");
                return;
            }
            if (FrameworkHelper.CurrentUser == null)
            {
                MsgHelper.ShowWarningMsgbox("您还没有登录？");
                return;
            }
            if (string.IsNullOrWhiteSpace(FrameworkHelper.CurrentUser.Login_Name))
            {
                MsgHelper.ShowWarningMsgbox("您目前是以单机用户登录系统，无需密码！");
                return;
            }
            if (!FrameworkHelper.CurrentUser.PSD.Equals(SecurityHelper.EnCryptUnidirectional(txtOldPwd.Text,AppConfig.SystemConfig.基本信息.密钥)))
            {
                MsgHelper.ShowWarningMsgbox("您输入的旧密码错误！");
                return;
            }
            try
            {
                string newpsd = SecurityHelper.EnCryptUnidirectional(txtpsd.Text.Trim(), AppConfig.SystemConfig.基本信息.密钥);
                bool suc = UserAccess.UpdatePSD(FrameworkHelper.CurrentUser.Login_Name, newpsd);
                if (suc)
                {
                    FrameworkHelper.CurrentUser.PSD = newpsd;
                    MsgHelper.ShowInfomationMsgbox("密码更新成功");
                }
                else
                {
                    MsgHelper.ShowErrorMsgbox("密码更新失败");
                    return;
                }
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowInfomationMsgbox("操作失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtpsd_TextChanged(object sender, EventArgs e)
        {
            int cnt = VerifyHelper.PasswordComplex(txtpsd.Text);
            foreach (StepItem itm in stpPsd.Items)
            {
                itm.Value = 0;
            }
            if (cnt < 2) ((StepItem)stpPsd.Items[0]).Value = 100;
            else ((StepItem)stpPsd.Items[cnt - 2]).Value = 100;
        }

    }
}